<?php
defined('APP_PATH') or die ('Unauthorized access');

class Model_Blog extends Model
{
	public function __construct()
	{
		$this->Config(array('table'=>'websys_notebook'));
	}
	
	/**
	 * 根据cid获取博客列表
	 * @param unknown_type $cid
	 * @param unknown_type $page
	 * @param unknown_type $perpage
	 */
	public function getBlogList($cid,$page,$perpage)
	{
		$where[] = array('is_blog','=',1);
		if($cid > 0){
			$where[] = array('cid','=',$cid);
		}
		$page = ($page - 1) * $perpage;
		$this->order("id DESC");
		$this->limit($page,$perpage);
		return $this->fetchAll($where);
	}
	
	/**
	 * 根据tag获取博客
	 * @param unknown_type $tags
	 * @param unknown_type $page
	 * @param unknown_type $perpage
	 */
	public function getBlogAsTags($tags,$page,$perpage)
	{
		$where[] = array('is_blog','=',1);
		$where[] = array('tags',' like ','%'.$tags.'%');
		$page = ($page - 1) * $perpage;
		$this->order("id DESC");
		$this->limit($page,$perpage);
		return $this->fetchAll($where);
	}
	
	/**
	 * 获取博客内容
	 * @param unknown_type $id
	 */
	public function getBlogInfo($id)
	{
		$where[] = array('is_blog','=',1);
		$where[] = array('id','=',$id);
		return $this->find($where);
	}
	
	/**
	 * 统计
	 * @param unknown_type $cid
	 */
	public function getBlogCount($cid,$tags='')
	{
		$where[] = array('is_blog','=',1);
		if($cid > 0){
			$where[] = array('cid','=',$cid);
		}
		if(!empty($tags)){
			$where[] = array('tags',' like ','%'.$tags.'%');
		}
		return $this->nums($where,array('id'));
	}
	
}